package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.Buckle;
import com.jemmyee.pizza.entity.CostOther;
import com.jemmyee.pizza.entity.Prepaid;
import com.jemmyee.pizza.service.IBuckleService;
import com.jemmyee.pizza.service.ICostOtherService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.BuckleBean;

@Controller
@RequestMapping("/admin/buckle")
public class BuckleController {
	private static Log log = LogFactory.getLog(BuckleController.class);
	@Autowired
	private IBuckleService<Buckle> buckleService;
	@Autowired
	private ICostOtherService<CostOther> costOtherService ;
	
	@RequestMapping("/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute BuckleBean bean,HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Buckle.class);
		BuckleBean buckleBean = new BuckleBean();
		if (from.equals("form")) {
			buckleBean = bean;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, buckleBean);
		}
		if (from.equals("page")) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				buckleBean = (BuckleBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
		}
		if (from.equals("menu")) {
			System.out.println("menu is call");
		}

		if(buckleBean.getName() != null && !buckleBean.getName().equals("")){
			dc.add(Restrictions.like("buckleName", buckleBean.getName(), MatchMode.ANYWHERE));
		}
		
//		if(buckleBean.getNumber() != null && !buckleBean.getNumber().equals("")){
//			dc.add(Restrictions.like("buckleId", buckleBean.getNumber(), MatchMode.ANYWHERE));
//		}
		
		if(buckleBean.getAddr() != null && !buckleBean.getAddr().equals("")){
			dc.add(Restrictions.like("buckleAddress", buckleBean.getAddr(), MatchMode.ANYWHERE));
		}
		// 发布时间
		if (buckleBean.getStartTime() != null && !buckleBean.getStartTime().equals("")) {
			dc.add(Restrictions.ge("createTime", buckleBean.getStartTime()));
		}
		if (buckleBean.getEndTime() != null && !buckleBean.getName().equals("")) {
			dc.add(Restrictions.le("createTime", buckleBean.getEndTime()));
		}

//		dc.addOrder(Order.desc("createTime"));
		ModelAndView mv = new ModelAndView();
		Page page = buckleService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"buckleId");
		mv.addObject("page", page);
		mv.addObject("buckleBean", buckleBean);
		mv.setViewName("/admin/buckle/list");
		return mv;
	}
	
	@RequestMapping("/view/{id}")
	public ModelAndView view(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		Buckle buckle = buckleService.findById(id);
		mv.setViewName("/admin/buckle/view");
		mv.addObject("buckle",buckle);
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id){
	   ModelAndView mv=new ModelAndView();
	   Buckle buckle = buckleService.findById(id);
	   mv.setViewName("/admin/buckle/edit");
	   mv.addObject("buckle",buckle);
	   return mv;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody Buckle buckle,HttpServletRequest request)
	{   
		System.out.println("cccc");
		Map<String, Object> model = new HashMap<String, Object>();
		Buckle b = buckleService.findById(buckle.getBuckleId());
		if(b == null){
			model.put("msg","error");
		}else{
			b.setBuckleName(buckle.getBuckleName());
			b.setBuckleType(buckle.getBuckleType());
			b.setBuckleTel(buckle.getBuckleTel());
			b.setBuckleCard(buckle.getBuckleCard());
			b.setBuckleAddress(buckle.getBuckleAddress());
			b.setBuckleBzm(buckle.getBuckleBzm());
			b.setRemark(buckle.getRemark());
			b.setCreateTime(buckle.getCreateTime());
			buckleService.update(b);
			model.put("msg","succ");
		}
		return model;
	}
	
	
	/*@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Integer id,HttpServletRequest request){
	    buckleService.delete(buckleService.findById(id));
	    return "redirect:/admin/buckle/list/0";
	}*/
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable Integer id,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		System.out.println(id);
		Buckle p = buckleService.findById(id);
		System.out.println(p);
		if(p == null){
			model.put("msg","error");
		}else{
			//System.out.println(p+"在这里 执行了 删除");
			buckleService.delete(p);
			model.put("msg","succ");
		}
		return model;
	}
}
